<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试WebSocket的使用</title>
</head>
<body>
    <input type="text" id="message">
    <button id="send-button">发送</button>
    <script>
        // 创建WebSocket实例
        let websocket = new WebSocket("ws://127.0.0.1:8080/test");
        websocket.onopen = function() {
            console.log("webSocket 连接成功!");
        }
        websocket.onclose = function() {
            console.log("webSocket 连接关闭!");
        }
        websocket.onerror = function() {
            console.log("webSocket 连接异常!");
        }
        websocket.onmessage = function(e) {
            console.log("webSocket 收到消息! " + e.data);
        }
        let messageInput = document.querySelector("#message");
        let sendButton = document.querySelector("#send-button");
        sendButton.onclick = function() {
            console.log("send: " + messageInput.value);
            websocket.send(messageInput.value);
        }
    </script>
</body>
</html>